package me.goldze.mvvmhabit.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;

import com.afollestad.materialdialogs.MaterialDialog;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import me.goldze.mvvmhabit.R;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;
import me.goldze.mvvmhabit.widget.CustomProgressDialog;


/**
 * Created by goldze on 2017/6/15.
 * 一个拥有DataBinding框架的基Activity
 * 这里根据项目业务可以换成你自己熟悉的BaseActivity, 但是需要继承RxAppCompatActivity,方便LifecycleProvider管理生命周期
 */

public abstract class BaseActivity<V extends ViewDataBinding, VM> extends RxAppCompatActivity implements IBaseActivity {
    private V binding;
    protected VM viewModel;
    private CustomProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new CustomProgressDialog();
        dialog.createLoadingDialog(this, "");
        initParam();

        initViewDataBinding();

        initData();

        initViewObservable();
        if (viewModel instanceof BaseViewModel) {
            ((BaseViewModel) viewModel).onCreate();

            ((BaseViewModel) viewModel).registerRxBus();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Messenger.getDefault().unregister(this);
        if (viewModel instanceof BaseViewModel) {
            ((BaseViewModel) viewModel).removeRxBus();
            ((BaseViewModel) viewModel).onDestroy();
        }
        viewModel = null;
    }

    /**
     * 注入绑定
     */
    private void initViewDataBinding() {
        //DataBindingUtil类需要在project的build中配置 dataBinding {enabled true }, 同步后会自动关联android.databinding包
        V viewDataBinding = DataBindingUtil.setContentView(this, initContentView());
        setBinding(viewDataBinding);
        getBinding().setVariable(initVariableId(), viewModel = initViewModel());
    }

    //刷新布局
    public void refreshLayout() {
        if (viewModel != null) {
            getBinding().setVariable(initVariableId(), viewModel);
        }
    }

    @Override
    public void initParam() {

    }

    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView();

    /**
     * 初始化ViewModel的id
     *
     * @return BR的id
     */
    public abstract int initVariableId();

    /**
     * 初始化ViewModel
     *
     * @return 继承BaseViewModel的ViewModel
     */
    public abstract VM initViewModel();

    @Override
    public void initData() {

    }

    @Override
    public void initViewObservable() {

    }

    public V getBinding() {
        return binding;
    }

    public void setBinding(V binding) {
        this.binding = binding;
    }

    public VM getViewModel() {
        return viewModel;
    }


    public void showDialog() {
        showDialog("请稍后...");
    }

    public void showDialog(String title) {
        if (dialog != null) {
            dialog.setMsg(title);
            if (!dialog.isShow())
                dialog.show();
        }
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
